/**
 * @file      product_media_font_adapt.c
 * @brief     product media font adapt
 *
 * Copyright (c) 2017 Huawei Tech.Co.,Ltd
 *
 * @author    HiMobileCam Reference Develop Team
 * @date      2018/3/17
 * @version   1.0

 */
#include "hi_mapi_osd.h"
#include "hi_product_res.h"
#include "hi_appcomm.h"


#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */

#define BYTE_BITS (8)
#define IsASCII(a) (((a)>=0x00&&(a)<=0x7F)?1:0)

/** OSD Fonts Lib */
static HI_U8* g_fontLib=NULL;
static HI_U32 g_fontWidth=0;
static HI_U32 g_fontHeight=0;
static HI_U32 g_fontType=0;/*(0-ASC/1-HZK)*/

static HI_S32 PDT_MEDIA_GetFontMod(HI_CHAR* Character,HI_U8** FontMod,HI_S32* FontModLen)
{
    /* Get Font Mod in GB2312 Fontlib*/
    HI_APPCOMM_CHECK_POINTER(Character, HI_FAILURE);
    HI_APPCOMM_CHECK_POINTER(FontMod, HI_FAILURE);
    HI_APPCOMM_CHECK_POINTER(FontModLen, HI_FAILURE);
    HI_U32 offset = 0;
    if (g_fontType==0) {
        if(!IsASCII(Character[0])) {
            return HI_FAILURE;
        }
        offset = (Character[0]-0x20)*(g_fontHeight*g_fontWidth/BYTE_BITS);
        *FontMod = (HI_U8*)g_fontLib+offset;
        *FontModLen = g_fontHeight*g_fontWidth/BYTE_BITS;
        return HI_SUCCESS;
    } else if (g_fontType==1) {
        HI_U32 areacode = 0;
        HI_U32 bitcode = 0;
        if(IsASCII(Character[0])) {
            areacode = 3;
            bitcode = (HI_U32)((HI_U8)Character[0]-0x20);
        } else {
            areacode = (HI_U32)((HI_U8)Character[0]-0xA0);
            bitcode = (HI_U32)((HI_U8)Character[1]-0xA0);
        }
        offset = (94*(areacode-1)+(bitcode-1))*(g_fontHeight*g_fontWidth/BYTE_BITS);
        *FontMod = (HI_U8*)g_fontLib+offset;
        *FontModLen = g_fontHeight*g_fontWidth/BYTE_BITS;
        return HI_SUCCESS;
    }
    return HI_FAILURE;
}

HI_S32 PDT_MEDIA_InitOsdFontLib(HI_VOID)
{
    /* Init OSD */
    HI_S32 ret = 0;
    HI_PDT_RES_DATA data;
    ret = HI_PDT_RES_GetData(HI_PDT_RES_PURP_OSDFONT, &data);
    if(ret != HI_SUCCESS) {
        return HI_MAPI_OSD_Init(NULL);
    } else {
        g_fontLib = data.memAddr;
        g_fontWidth = data.arg[0];
        g_fontHeight = data.arg[1];
        g_fontType = data.arg[2];
        HI_MAPI_OSD_FONTS_S stOsdFonts;
        stOsdFonts.u32FontWidth = g_fontWidth;
        stOsdFonts.u32FontHeight = g_fontHeight;
        stOsdFonts.pfnGetFontMod = PDT_MEDIA_GetFontMod;
        return HI_MAPI_OSD_Init(&stOsdFonts);
    }
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */

